// page/receive_object_add/index4.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        customItem:'全部',
        region:[],
        receiver:'张三',
        phone:'1234567891',
        area:'xxx'
    },

    // input(e){
    //     let data={}
    //     data[e.currentTarget.dataset.key]=e.detail.value
    //     this.setData(data)
    // },
    input1(e){
        this.setData({
            receiver:e.detail.value
        })
    },
    input2(e){
        this.setData({
            phone:e.detail.value
        })
    },
    input3(e){
        this.setData({
            area:e.detail.value
        })
    },

    bindRegionChange:function(e){
        this.setData({
            region:e.detail.value
        })
    },

    submit(){
        // let regex={
        //     receiver:/^[\u2E80-\u9FFF]+$/,
        //     phone:/^1\d{10}$/
        // }    
        let address={
            memberId:wx.userInfo._id,
            receiver:this.data.receiver,
            phone:this.data.phone,
            area:this.data.area,
            city:this.data.region.join('')
        }
        if(!address.receiver || /^[\u2E80-\u9FFF] + $/.test(address.receiver)){
            wx.showToast({
              title: '收货人错误',
              icon:'success',
              duration:2000
            })
            return false
        }
        if(!address.phone|| /^1\d{11}$/.test(address.phone)){
            wx.showToast({
              title: '清输入正确的电话号码',
              icon:'success',
              duration:2000
            })
            return false
        }
        if(!address.city){
            wx.showToast({
              title: '清输入地址',
              icon:'success',
              duration:2000
            })
            return false
        }
        wx.post('http://localhost:3000/wx/address',address).then(res=>{
            if(!res.errosMsg){
                wx.showToast({
                  title: '新增成功',
                  icon:'success',
                  duration:2000
                })
                setTimeout(()=>{
                    wx.navigateBack({
                 delta: -1,
                 success: (res) => {},
                 fail: (res) => {},
                 complete: (res) => {},
               })
               },2000)
            }
            else{
                wx.showToast({
                  title: res.errosMsg,
                  icon:'fail',
                  duration:2000
                })

            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
  
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})